package com.zdsoft.blp.zongbuju.wode.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.MainActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/1.
 */
public class DengLuActivity extends BaseActivity implements View.OnClickListener,NetCallback{
    private EditText et_uesename,et_paaword;
    private Button bt_login;
    private TextView tv_zhmm;
    private LinearLayout ll_zhuce;
    private static  final  Integer LOGIN =1;
    private SharedPreferences sp;
    private boolean isBack =false;
    @Override
    protected void init() {
        setContentView(R.layout.denglu_wode);
        //实例化控件
        et_uesename = find(R.id.et_uesename);
        et_paaword = find(R.id.et_paaword);
        bt_login = find(R.id.bt_login);
        tv_zhmm = find(R.id.tv_zhmm);
        ll_zhuce = find(R.id.ll_zhuce);
        sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        isBack =getIntent().getBooleanExtra("isBack",false);
        bt_login.setOnClickListener(this);
        tv_zhmm.setOnClickListener(this);
        ll_zhuce.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent =null;
        switch (v.getId()){
            case R.id.bt_login:
                String username = et_uesename.getText().toString().trim();
                String password = et_paaword.getText().toString().trim();
                Net net = new Net();
                net.url= API.LOGIN_URL;
                net.nameSpace= API.LOGIN_NAMESPACE;
                net.methodName = API.LOGIN;
                Map<String,Object> map = new HashMap<>();
                map.put("name", username);
                map.put("pwd",password);
                net.parms =map;
                net.code=LOGIN;
                getServer(net,DengLuActivity.this);
                break;
            case R.id.tv_zhmm:
                intent = new Intent(getApplicationContext(),ZhaoHuiMiMaActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_zhuce:
                intent = new Intent(getApplicationContext(),ZhuCeActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if(flag&&code==LOGIN){
            try {
                JSONObject object = new JSONObject(res);
                boolean isLoginSuccess = object.getBoolean("state");
                if(isLoginSuccess){
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putBoolean("isLoginSuccess",true);
                    editor.putString("userId",object.getString("userId"));
                    editor.putString("communityName", object.getString("communityName")); //小区
                    editor.putString("communityCode", object.getString("communityCode")); //小区编号
                    editor.commit();
                    if(isBack){
                        DengLuActivity.this.finish();
                    }else {
                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                    }
                }else {
                    Toast.makeText(getApplicationContext(),object.getString("msg").toString(),Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
